<template>
  <div id="dcontent" class="mui-content">
    <div class="mui-content-padded">
      <div class="mui-content-padded oauth-area">
        <div id="UN_WEB" class="mui-btn mui-btn-blue mui-btn-block pay">
          豪车之家订单未支付平台
        </div>
      </div>
      <ul class="mui-table-view">
        <li class="mui-table-view-cell">
          订&nbsp;&nbsp;单&nbsp;&nbsp;号:
          <span id="o_serial">{{ unbills.orderId }}</span>
        </li>
        <li class="mui-table-view-cell">
          支付金额:
          <span id="o_money"
            >{{
              unbills.money |  numFilter
            }}万</span
          >
        </li>
        <li class="mui-table-view-cell">
          会员类型: <span id="v_name ">{{ viplist.v_name }}</span>
        </li>
        <li class="mui-table-view-cell">
          折扣:<span id="o_money">{{ viplist.v_discount }}折</span>
        </li>

        <li class="mui-table-view-cell">
          下单时间:<span id="o_date">{{
            unbills.data | dateFmt("YYYY-MM-DD")
          }}</span>
        </li>
        <li class="mui-table-view-cell">
          收货地址:
          <textarea
            name=""
            id="o_address"
            v-model="o_address"
            cols="20"
            rows="5"
          ></textarea>
        </li>
        <li class="mui-table-view-cell">
          备注:<textarea
            name=""
            id="o_address"
            v-model="o_comment"
            cols="20"
            rows="5"
          ></textarea>
        </li>
      </ul>
      <button
        type="button"
        class="mui-btn mui-btn-success mui-btn-block mui-btn-outlined"
        @click="ordertable"
      >
        提交订单
      </button>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import moment from "moment";
export default {
  filters: {
    dateFmt: function (str, pattern) {
      return moment(str).format("YYYY-MM-DD HH:mm:ss");
    },
     numFilter (value) {
    // 截取当前数据到小数点后两位
    let realVal = parseFloat(value).toFixed(2)
    return realVal
  }
},
 
  data() {
    return {
      orderId: "0",
      o_address: "",
      o_comment: "",
      money: "0",
      data: 0,
      unbills: "",
      u_money: "",
      viplist: "",
    };
  },
  methods: {
    ordertable() {
      let ordertable = {
        o_serial: this.unbills.orderId,
        o_money:  this.unbills.money,
        o_date: this.unbills.data,
        o_address: this.o_address,
        o_u_id: this.uid,
        o_p_id: this.unbills.pids,
        o_comment: this.o_comment,
      };  
      this.$ajax({
        url: "before/addordertable",
        method: "post",
        data: ordertable,
      }).then((resp) => {

      });
      this.$ajax({
        url: "users/getUser/" + this.uid,
        method: "get",
      }).then((resp) => {
        this.u_money = resp.data.message[0].u_money;
        // 折扣
        this.money = this.unbills.money - this.unbills.money * this.viplist.v_discount * 0.01;
        // 应付
        this.u_money = parseFloat(this.u_money) - parseFloat(this.o_money);
        if (this.u_money <= 0) {
          Toast("余额不足");
          return;
        }
        let person = {
         ' u_money': this.u_money,
          'u_id': this.uid,
        }
         this.$ajax({
          url: "before/users",
          method: "put",
          data: person,
        }).then((resp) => {
          localStorage.setItem("unbills", "");
          this.$router.push("/bill/Addbill/" + this.uid);
        });
      });
    },
    getvip() {
      this.$ajax({
        url: "before/uvinpkind/" + this.uid,
        method: "get",
      }).then((resp) => {
        this.viplist = resp.data.message[0];
      });
    },
  },
  created() {
    try {
      let uid = JSON.parse(localStorage.getItem("uid"));
      this.uid = uid;
      let unbills = JSON.parse(localStorage.getItem("unbills"));
      this.unbills = unbills;
    } catch (error) {
      Toast("没有未完成订单");
      this.$router.go(-1);
    }
    this.getvip();
  },
};
</script>

<style scoped>
</style>